//
//  CommentTableViewCell.m
//  SoundPill
//
//  Created by 宋旭东 on 15/8/26.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIViewExt.h"
@interface CommentTableViewCell ()
{
    
}
@property (strong, nonatomic) UIView *line;
@property (strong, nonatomic) UIImageView *avatar;
@property (strong, nonatomic) UILabel *name;
@property (strong, nonatomic) UILabel *toName;
@property (strong, nonatomic) UILabel *commentTime;
@property (strong, nonatomic) UIButton *respond;
@end

@implementation CommentTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.avatar = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self addSubview:_avatar];
    _avatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapUserAvatar:)];
    [_avatar addGestureRecognizer:tap];
    
    self.name = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_name];
    _name.userInteractionEnabled = YES;
    UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapUserAvatar:)];
    [_name addGestureRecognizer:nameTap];
    self.toName = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_toName];
    self.content = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_content];
    self.respond = [UIButton buttonWithType:UIButtonTypeCustom];
    [_respond setTitle:@"回复" forState:UIControlStateNormal];
    [_respond setTitleColor:[UIColor colorWithRed:0.717 green:0.719 blue:0.727 alpha:1.000] forState:UIControlStateNormal];
    _respond.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_respond];
    self.commentTime = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:_commentTime];
    self.line = [[UIView alloc] initWithFrame:CGRectZero];
    [self addSubview:_line];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
//    评论-用户头像
    _avatar.frame = CGRectMake(10, 12, ([UIScreen mainScreen].bounds.size.height - 64 - 45) / 8 - 24, ([UIScreen mainScreen].bounds.size.height - 64 - 45) / 8 - 24);
    [_avatar setImageWithURL:[NSURL URLWithString:_model.avatar] placeholderImage:[UIImage imageNamed:@"default_head"]];
//    评论-用户名
    UIFont *nameFont = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    _name.font = nameFont;
    _name.text = _model.username;
    _name.textColor = [UIColor blackColor];
    CGSize nameSize = [_name.text boundingRectWithSize:CGSizeMake(self.frame.size.width,_avatar.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:nameFont}  context:nil].size;
    [_name sizeThatFits:CGSizeMake(self.frame.size.width,_avatar.frame.size.height)];
    _name.frame = CGRectMake(_avatar.right + 10, _avatar.top, nameSize.width, _avatar.height / 2);
//    评论-回复用户名
    NSString *contentStr;
    if (_model.tousername.length > 0) {
        contentStr = [NSString stringWithFormat:@"@%@：%@",_model.tousername,_model.content];
    } else {
        contentStr = _model.content;
    }
   
//    评论-内容
    UIFont *contentFont = [UIFont systemFontOfSize:14];
    _content.font = contentFont;
    _content.text = contentStr;
    _content.numberOfLines = 0;
    _content.textColor = [UIColor grayColor];
    CGRect rect = [contentStr boundingRectWithSize:CGSizeMake(self.frame.size.width - 50 - 2 * _avatar.frame.size.width, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:contentFont} context:nil];
    _content.frame =  CGRectMake(_name.frame.origin.x, _name.frame.origin.y + _name.frame.size.height + 5, rect.size.width,  rect.size.height);
    
    _respond.userInteractionEnabled = NO;
//    评论-时间
    _commentTime.frame = CGRectMake(_avatar.right + 10, _name.top,  self.frame.size.width - (_avatar.right + 20), _avatar.height / 2);
    _commentTime.textAlignment = NSTextAlignmentRight;
    _commentTime.font = [UIFont systemFontOfSize:13];
    _commentTime.textColor = [UIColor colorWithRed:0.717 green:0.719 blue:0.727 alpha:1.000];
    if (![_model.reviewtime isEqualToString:@"一秒前"]) {
        NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorian setFirstWeekday:2];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *fromDate;
        NSDate *toDate;
        [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:_model.reviewtime]];
        [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:[NSDate date]];
        NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
        if (dayComponents.day == 0) {
            //        显示今天
            _commentTime.text = [_model.reviewtime substringFromIndex:11];
        } else if (dayComponents.day == 1) {
            //        显示昨天
            _commentTime.text = @"昨天";
        } else if (dayComponents.day < 7) {
            //        显示星期
            NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
            NSDate *date = [dateFormatter dateFromString:_model.reviewtime];
            NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
            NSDateComponents *theComponents = [gregorian components:calendarUnit fromDate:date];
            _commentTime.text = weekdays[theComponents.weekday];
        } else{
            _commentTime.text = [_model.reviewtime substringToIndex:10];
        }
    } else {
        _commentTime.text = _model.reviewtime;
    }
    
    
    
    
    
    
    
    
    
    
    self.line.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 1);
    _line.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.087];
}
- (void)tapUserAvatar:(UITapGestureRecognizer *)recognizer
{
    //判断是否是用户自己
    if ([_model.user_id isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"]]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"commentListGoToUser" object:nil];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"commentListGoToOther" object:_model];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
